package com.zlb.app.utils;


import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author zWX1367238
 * @Since 2024-08-14 15:21:15
 */
public class CopyUtil {

    /**
     * 对象属性复制
     * @param source 资源对象
     * @param target 目标对象泛型
     * @return
     * @param <T> 泛型
     */
    public static <T> T copyObject(Object source, Class<T> target) {
        T temp = null;
        try {
            temp = target.newInstance();
            if (null != source) {
                org.springframework.beans.BeanUtils.copyProperties(source, temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    // 集合复制
    public static <T, S> List<T> copyList(List<S> source, Class<T> target) {
        List<T> list = new ArrayList<>();
        if (null != source && source.size() > 0) {
            for (Object obj : source) {
                list.add(CopyUtil.copyObject(obj, target));
            }
        }
        return list;
    }

}